import jdk.nashorn.internal.ir.CallNode;

class Node {
    public int val;
    public Node next;
    public Node(int val) {
        this.val = val;
    }
}

public class  Linkedlist {
    public Node head = null;

    /**
     * 穷举 创建链表
     */
    public void creatList() {
        Node node1 = new Node(2);
        Node node2 = new Node(7);
        Node node3 = new Node(8);
        Node node4 = new Node(9);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }

    /**
     * 头插法
     */
    public void addFrist(int val) {
        Node node = new Node(val);
        node.next = head;
        this.head = node;
    }

    public void show() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void show2(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    /**
     * 尾插法
     */
    public void addLast(int val) {
        Node node = new Node(val);
        Node cur = this.head;
        if (this.head == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /**
     * 任意位置插入,第一个数据节点为0号下标
     */
    public void addIntex(int index, int data) {
        if (index < 0 || index > getLength()) {
            System.out.println("不合法！");
            return;
        }
        if (index == 0) {
            addFrist(data);
            return;
        }
        if (index == getLength()) {
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node ret = searchIndexSubOne(index);  //调用search 找到index-1 位置
        node.next = ret.next;
        ret.next = node;
    }

    public int getLength() {
        int length = 0;
        Node cur = this.head;
        while (cur != null) {
            length++;
            cur = cur.next;
        }
        return length;
    }

    public Node searchIndexSubOne(int index) {
        Node cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 删除第一次出现关键字为key的节点
     */
    public void remove(int key) {
        //1.判断头节点是不是要删除的key
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        //2.找key前驱节点
        Node pre = searchPrev(key);
        if (pre == null) {
            System.out.println("没有要删除的节点");
            return;
        }
        Node del = pre.next;
        pre.next = del.next;
    }

    //找key的前驱节点
    public Node searchPrev(int key) {
        Node cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    /**
     * 删除所有值为key的节点
     */
    public void removeAllKey(int key) {
        Node cur = this.head.next;
        Node pre = this.head;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }

    public void clear() {
        //  1
        //this.head = null;
        Node cur = this.head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }

    /**
     * 反转一个单链表
     */
    public Node reverseList1() {
        if (this.head == null) {
            return this.head;
        }
        Node cur = this.head;
        Node curNext = cur.next;
        cur.next = null;
        cur = curNext;
        while (cur != null) {
            curNext = cur.next;
            cur.next = this.head;
            this.head = cur;
            cur = curNext;
        }
        return this.head;
    }

    /**
     * 给定一个头结点为 head 的非空单链表，返回链表的中间结点。
     * 如果有两个中间结点，则返回第二个中间结点。
     */
    public Node middleNode() {
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 输入一个链表，输出该链表中倒数第k个结点。
     */
    public Node findKthToTail(int k) {
        if (k <= 0) {
            return null;
        }
        Node fast = this.head;
        Node slow = this.head;
        while (k - 1 != 0) {
            if (fast.next != null) {
                fast = fast.next;
                k--;
            } else {
                System.out.println("k太大");
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 给一定值x，编写一段代码将所有小于x的结点排在其余结点之前，
     * 且不能改变原来的数据顺序，返回重新排列后的链表的头指针。
     */
    public Node partition(int x) {
        Node cur = this.head;
        Node bs = null;
        Node be = null;
        Node as = null;
        Node ae = null;
        while (cur != null) {
            //val 小于 x时
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {   //大于等于x时
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }

            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;


        }
        return bs;
    }














}


